package Modelo;

/*
 *  Proyecto eJAM [G12]
 *  Miembros:
 *      - Javier Ruiz Hidalgo
 *      - Adrian Ramos Cápitas
 *      - Miguel Alejandro Esteban Ordóñez
 */

/*
SHA1 sha = SHA1.getInstance();
sha.getHash("clave");
 */

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA1 {
    private MessageDigest md;
    private byte[] buffer, digest;

    private static SHA1 INSTANCE = null;

    private SHA1() {
    }

    public static SHA1 getInstance() {
        if (INSTANCE == null)
            INSTANCE = new SHA1();
        return INSTANCE;
    }

    public String getHash(String message) throws NoSuchAlgorithmException {
        buffer = message.getBytes();
        md = MessageDigest.getInstance("SHA1");
        md.update(buffer);
        digest = md.digest();
        String hash = "";

        for(byte aux : digest) {
            int b = aux & 0xff;
            if (Integer.toHexString(b).length() == 1) hash += "0";
            hash += Integer.toHexString(b);
        }
        return hash;
    }
}